package com.example.mongo.controller;

import com.example.mongo.Model.bean.Chapter;
import com.example.mongo.Model.bean.Content;
import com.example.mongo.Model.bean.User;
import com.example.mongo.Model.dto.ResponseData;
import com.example.mongo.service.ChapterService;
import com.example.mongo.service.ContentService;
import com.example.mongo.service.UserService;
import com.example.mongo.utils.CookieUtils;
import com.example.mongo.utils.RedisUtil;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/content")
public class ContentController {
    @Autowired
    ContentService contentService;
    @Autowired
    ChapterService chapterService;
    @Autowired
    ResponseData responseData;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    UserService userService;

    @RequestMapping(value = "/getContentByChapterId/{chapterId}", produces = "application/json;charset=UTF-8",method = RequestMethod.GET)
    public String getChapterByChapterId(@PathVariable("chapterId")int chapterId, HttpServletRequest request){
        Chapter chapter = chapterService.getChapterByChapterId(chapterId);

        List<Content> list = contentService.getContentByChapterId(chapterId);
        if(null==chapter&&null==list){
            responseData.error("400","无此章节","");
            return responseData.toString();
        }

        if (chapter.getIsFree()==1){   //免费章节所有人可以看
            responseData.success("ok", JSONArray.fromObject(list).toString());
            return responseData.toString();
        }

        String id = CookieUtils.isLogin(request,redisUtil);
        if ("false"==id){                                                   //收费章节未登录
            responseData.error("400","请登录","");
            return responseData.toString();
        }

        User user = userService.getUserById(id);

        //isVIP {0:普通用户，1：VIP用户，2 管理员}  未使用Shrio 等安全工具

        if(user.getIsVIP()==0&&(user.getUserCoin()-10)>0){                  //普通用户金币足够
            user.setUserCoin(user.getUserCoin()-10);                        //扣除金币。统一每章节10金币
            userService.updateUserCoin(user);

            responseData.success("ok", JSONArray.fromObject(list).toString());
            return responseData.toString();
        }

        if(user.getIsVIP()==1&&user.getDeadLine()>new Date().getTime()||user.getIsVIP()==2){                  //VIP 免费看 ,且未过期
            responseData.success("ok", JSONArray.fromObject(list).toString());
        }else {
            user.setUserCoin(user.getUserCoin()-10);                        //vip过期 扣除金币。统一每章节10金币
            userService.updateUserCoin(user);

            responseData.success("会员已过期", JSONArray.fromObject(list).toString());
        }


        return responseData.toString();
    }



}
